#!/bin/bash

#export INPUT_APP=ibus

#export XMODIFIERS="@im=sicm"
#export GTK_IM_MODULE=scim
#export QT_IM_MODULE=scim
#----------------------------
#   export LANG=zh_CN.utf8
#   export LC_CTYPE=zh_CN.utf8
#   export LANGUAGE=zh_CN.utf8
#   export LC_TIME=zh_CN.utf8
#----------------------------
#gconftool-2 -s -t string /desktop/gnome/session/required_components/windowmanager openbox
#gconftool-2 --type bool --set /apps/nautilus/preferences/show_desktop false
#USE_PANEL="fbpanel"  #enable panel
#USE_PANEL="lxpanel"  #enable panel
USE_PANEL="xfce4-panel"  #enable panel
#USE_PANEL="gnome-panel"  #enable panel
USE_CONKY=1
#CONKY_SHORT=${CONKY_SHORT:-0}
CONKY_SHORT=1
USE_ROX=0
USE_LXDE=1
USE_XFDESK=0
USE_MCS=1
USE_CPUFREQ=0
#USE_LAPTOP=1
USE_GNOME_SCREENSAVER=1
DEFAULT_FREQ=1600000
#----------------------------
USE_LAPTOP=${USE_LAPTOP:-0}
echo "USE_LAPTOP = ${USE_LAPTOP}"
#----------------------------
export LANGUAGE="zh_CN:zh:en_US:en"
export LC_CTYPE="zh_CN.UTF-8"
export LANG="zh_CN.UTF-8"
export LC_TIME="zh_CN.UTF-8"
#----------------------------
#export   LANG=en_US.utf8
#   export   LC_CTYPE=zh_CN.utf8
#   export   LC_NUMERIC="en_US.utf8"
#   export   LC_TIME="en_US.utf8"
#   export   LC_COLLATE=zh_CN.utf8
#   export   LC_MONETARY="en_US.utf8"
#   export   LC_MESSAGES="en_US.utf8"
#   export   LC_PAPER="en_US.utf8"
#   export   LC_NAME="en_US.utf8"
#   export   LC_ADDRESS="en_US.utf8"
#   export   LC_TELEPHONE="en_US.utf8"
#   export   LC_MEASUREMENT="en_US.utf8"
#   export   LC_IDENTIFICATION="en_US.utf8"

#http://www.linuxsir.org/bbs/attachment.php?attachmentid=46627&d=1204610909 出了conky上的一些信息其他一样。
#----------------------------
function ck_prog() {
    if [ $# -lt 1 ] ; then
        echo "please use args"
        return 1
    fi

    program=$1
    if [ -z `pidof ${program}` ] ; then
        echo "runing ${program}"
        return 0
    else 
        echo "${program} is running"
        return 1
    fi
}

#===========================================================
# 输入法 （Archlive 输入法预配置）
if [ "x${INPUT_APP}" = "x" ]; then
	export INPUT_APP=fcitx
fi
# 输入法选项 INPUT_APP 优先在 ~/.bash_profile 或者其他先启动的
# 配置文件中中作为全局变量指定
# INPUT_APP 目前可选项：
# fcitx: 小企鹅输入法
# yong: 小小输入法
# ibus: IBUS——新一代输入框架，支持拼音、五笔、二笔等等 
# scim： SCIM--传统输入法
#=======================================
case ${INPUT_APP} in
	fcitx) # 使用小企鹅输入法
		export XIM=fcitx
		export XMODIFIERS="@im=fcitx"
		export XIM_PROGRAM="/usr/bin/fcitx"
		export GTK_IM_MODULE="xim"
		export QT_IM_MODULE="xim"
		export XIM_ARGS="-d"
		[ -z "$(pidof fcitx)" ] && (fcitx &)
		;;
	yong) # 使用小小输入法
		export XIM=yong
		export XMODIFIERS="@im=yong"
		export XIM_PROGRAM="/usr/bin/yong"
		export GTK_IM_MODULE="xim"
		export QT_IM_MODULE="xim"
		export XIM_ARGS="-d"
		[ -z "$(pidof fcitx)" ] && (yong -d &)
		;;
	ibus) # 使用ibus输入框架
		export XMODIFIERS="@im=ibus"
		export GTK_IM_MODULE="ibus"
		export QT_IM_MODULE="ibus"
		export XIM=ibus
		export XIM_PROGRAM=/usr/bin/ibus-daemon
		export XIM_ARGS="-d -x"
		pkill -9 ibus-daemon
		ibus-daemon -d -x 
		#ibus&
		;;
	scim) # 使用scim输入框架
		export XIM=scim
		export XMODIFIERS="@im=scim"
		export XIM_PROGRAM="/usr/bin/scim"
		export GTK_IM_MODULE="scim-bridge"
		export QT_IM_MODULE="scim-bridge"
		export XIM_ARGS="-d"
		[ -z "$(pidof scim)" ] && (scim-bridge &)
		;;
	*) # 暂时没有该输入法的配置文件，请自行添加
		echo "暂时没有该输入法的配置文件，请自行添加"
		;;
	esac
#===========================================================
#=======================================
#
#----------------------------
#scim -f socket -c socket -d
#----------------------------
# Startup Background Sound
#mplayer /home/oceanboo/Program/Startup_Sound_Mandriva.wav &
#Force OpenOffice.org to use GTK theme
export OOO_FORCE_DESKTOP=gnome

# . $GLOBALAUTOSTART
# Xmodmap
if [ -e ~/.Xmodmap ] ; then
    echo "xmodmap ~/.Xmodmap"
    xmodmap ~/.Xmodmap
fi

# Xresources
if [ -e ~/.Xresources ] ; then
    echo "xrdb -merge ~/.Xresources"
    xrdb -merge ~/.Xresources
fi 

#xmodmap -e "keycode 115 = Super_L" -e "add mod4 = Super_L"



#if [ -z `pidof urxvtd` ] ; then
    #killall urxvtd
#else
    #exec urxvtd&
#fi
if [ `pidof urxvtd|wc -w` -eq 0 ] ; then
    exec urxvtd&
#else
#    killall urxvtd
#    exec urxvtd&
fi

#   if [ `pidof fcitx|wc -w` -eq 0 ] ; then
#       exec fcitx&
#   #else
#   #    killall fcitx
#   #    exec fcitx&
#   fi
#   #


#ibus &

#for i in $(seq 6); do /usr/bin/setleds -D +num < /dev/vc/${i} >/dev/null; done ###Clock Number

#idesk&
#安装numlockx软件包： 
#pacman -S numlockx
#numlockx on
# killall tilda > /dev/null 2>&1
# tilda&
#
#=============================================
if [ "x${PANEL_APP}" = "x" ]; then
	export PANEL_APP=xfce4-panel
    #export PANEL_APP=gnome-panel
fi

case ${PANEL_APP} in
	lxpanel) # 使用
		[ -z "$(pidof lxpanel)" ] && (lxpanel &)
		;;
    fbpanel)
        [ -z "$(pidof fbpanel)" ] && (fbpanel &)
        ;;
    gnome-panel)
        [ -z "$(pidof gnome-panel)" ] && (gnome-panel&)
        ;;
    xfce4-panel)
        [ -z "$(pidof xfce4-panel)" ] && (xfce4-panel&)
        ;;
esac


#=============================================
if [ $USE_MCS = 1 ] ; then 
    if [ -z `pidof xfce-mcs-manager` ] ; then
        type -p xfce-mcs-manager
        if [ $? -eq 0 ] ; then
            xfce-mcs-manager &
        fi
    fi
fi 
#xfce4-panel&
#(sleep 2 && xfce4-panel) &
#(./startrun.sh)&

if [ -z `pidof gkrellm` ] ; then
    #killall gkrellm > /dev/null 2>&1
    echo "test gkrellm"
fi
#gkrellm&
#-------------------------
xset b off  #terminal bell off
#-------------------------
#-------------------------------------------------------------------------
# turn off screen blanking and turn on energy star features
#xset s off           
#xset dpms 0 0 3600  # turn off monitor
#xset r rate 240 90  # until repeat in millisec begins /  hz-repeat rate then

#-------------------------------------------------------------------------

#set wallpager
#feh --bg-scale ~/Pictures/bgimage
#feh --bg-scale ~/wallpager/bg.jpg

#set wallpager
#feh --bg-scale ~/Pictures/bgimage
#feh --bg-scale ~/wallpager/bg.jpg
#feh --bg-scale /usr/share/fvwm-crystal/fvwm/wallpapers/
#feh --bg-scale ~/myhome/office/wallpagers/summer.jpg
#-------------------------------------------------------------------------
#feh --bg-scale ~/MultiConfigDir/vladstudio_oxygen_4_1024x768.jpg
#if [ -z `pidof conky` ] ; then
if [ $USE_CONKY = 1 ] ; then
    if [ `pidof conky|wc -w` -eq 0 ] ; then
        killall conky > /dev/null 2>&1
        echo "kill runing conky"
    else
        echo "run conky"
    fi
    conky -d
#   if [ ${CONKY_SHORT} = 1 ] ; then
#       if [ -e ${HOME}/MultiConfigDir/conky/oceanboo_conkyrc ] ; then
#           conky -c ${HOME}/MultiConfigDir/conky/oceanboo_conkyrc -d
#       else 
#           conky -d
#       fi
#   else 
#       conky -c ${HOME}/MultiConfigDir/conky/myconkyrc -d
#   fi
fi


if [ "x${DESKTOP_APP}" = "x" ]; then
	export DESKTOP_APP=pcmanfm
fi

case ${DESKTOP_APP} in
	pcmanfm) # 使用
        #pcmanfm -d &
		[ -z "$(pidof pcmanfm)" ] && (pcmanfm --desktop &)
		;;
	xfdesktop) # 使用小小输入法
		[ -z "$(pidof xfdesktop)" ] && (xfdesktop &)
        ;;
esac


if [ $USE_CPUFREQ -eq 1 ] ; then
    SET_CPUFREQ_SH="${HOME}/MultiConfigDir/thinkpad/scripts/set_cpufreq.sh"
    if [ -e ${SET_CPUFREQ_SH} ] ; then
        . ${SET_CPUFREQ_SH}
    fi
fi


#   CUR_FREQ=0
#   if [ $USE_CPUFREQ -eq 1 ] ; then
#       #echo "USE_CPUFREQ -eq 1" 
#       CUR_FREQ=$(cpufreq-info -f)
#       #echo "CUR_FREQ = $CUR_FREQ"
#       #echo "DEFAULT_FREQ = ${DEFAULT_FREQ}"
#       if [ ${DEFAULT_FREQ} -ne ${CUR_FREQ} ] ;then
#           sudo cpufreq-set -g userspace
#           sudo cpufreq-set -d 800Mhz
#           sudo cpufreq-set -u 2.0GHz
#           sudo cpufreq-set -f 1.2GHz
#           #echo "set current freq = ${DEFAULT_FREQ}"
#       #else
#           #echo "current freq -eq ${DEFAULT_FREQ}"
#       fi
#   #else
#       #echo "USE_CPUFREQ -ne 1" 
#   fi
#

#关闭触摸块
#x11-drivers/synaptics (/usr/bin/synclient)
if [ ${USE_LAPTOP} -eq 1 ] ; then
    echo "synclient TouchpadOff=1"
    synclient TouchpadOff=1 &
fi


### Gnome 相关
## 启动 gnome-settings-daemon 来设置 Gnome 程序主题,字体等
#gnome-settings-daemon &
## 启动 nautilus 来显示桌面图标和桌面背景 ( 要启动 gnome-settings-daemon )
#nautilus &
## 用 gnome-volume-manager 来自动挂载 U 盘等
if [ -e /usr/libexec/gnome-volume-manager ] ; then
    sudo pkill gnome-volume-manager 
    sudo /usr/libexec/gnome-volume-manager &
fi

#gnome-volume-manager &
if [ -z $POWER_APP ] ; then
    export POWER_APP=xfce4
fi
case ${POWER_APP} in
	gnome) # 使用小企鹅输入法
        sudo pkill -9 gnome-power-manager
        sudo gnome-power-manager &
        ;;
    xfce4)
        sudo pkill -9 xfce4-power-manager
        sudo xfce4-power-manager &
        ;;

    *)
        sudo pkill -9 xfce4-power-manager
        sudo xfce4-power-manager &
        ;;

esac


#   ck_prog  gnome-power-manager 
#   if [ $? -eq 0 ] ; then
#       sudo gnome-power-manager &
#       #电源管理可以设置 lcd 亮度
#   fi
#sudo gvfsd &
#sudo gvfs-fuse-daemon ~/.gvfs &
#sudo /usr/lib/gnome-session/helpers/gnome-settings-daemon-helper &

#gvfs-gdu-volume-monitor
#gvfs-gphoto2-volume-monitor


if [ ${USE_GNOME_SCREENSAVER} -eq 1 ] ; then
    sudo pkill -9 gnome-screensaver
    gnome-screensaver &
    echo "gnome-screensave no running"
else
## xscreensaver 屏幕保护程序
    #xscreensaver -no-splash &
    killall xscreensaver ; nohup xscreensaver -nosplash >/dev/null 2>/dev/null &
fi

function my_arch_set_lcd_br() {
    if [ $# -eq 1 ] ; then
        sudo sh -c "echo $1 > /proc/acpi/video/VID1/LCD0/brightness"
    else
        sudo sh -c "echo 60 > /proc/acpi/video/VID1/LCD0/brightness"
    fi
}

my_arch_set_lcd_br 60


#stalonetray -i 16 --max-width 48 --icon-gravity E --geometry 48x16-0+0 -bg '#2e3436' --sticky --skip-taskbar &

### Xfce 相关
#xfce-mcs-manager &

### 启动其它您想让 IceWM 自动启动的程序
#stardict -h & # 星际译王
#glipper &     # 多重剪贴板
#kmix &        # K音量

#-------------------------------------------------------------------------
#feh --bg-scale ~/myhome/office/wallpagers/fresh-morning.jpg
#feh --bg-scale /usr/share/fvwm-crystal/fvwm/wallpapers/fvwm-crystal-dark1-1024x768.jpg
#feh --bg-scale ~/wallpager/bg1.jpg

#hsetroot -full ~/wallpaper/bg.jpg &
#hsetroot -full ~/wallpager/openbox.png&


#Transparent windows
#xcompmgr -c -t-5 -l-5 -r4.2 -o.55 &

#killall wmcpuload > /dev/null 2>&1
# wmcpuload&

#killall wmnetload > /dev/null 2>&1
# wmnetload&

#killall wmmemload > /dev/null 2>&1
# wmmemload&

#for i in $(seq 6); do 
    #/usr/bin/setleds -D +num < /dev/vc/${i} >/dev/null; 
#done

# Run the system-wide support stuff
#. $GLOBALAUTOSTART

# Programs to launch at startup
#hsetroot ~/wallpaper.png &
#xcompmgr -c -t-5 -l-5 -r4.2 -o.55 &

# SCIM support (for typing non-english characters)
#export LC_CTYPE=ja_JP.utf8
#export XMODIFIERS=@im=SCIM
#export GTK_IM_MODULE=scim
#export QT_IM_MODULE=scim
#scim -d &

# Programs that will run after Openbox has started
#(sleep 2 && fbpanel -p myfbpanel) &

#   DELAY=.75
#   APPS='/home/mwilson/bin/clock \
#   /home/mwilson/bin/weather \
#   /home/mwilson/bin/grabimage \
#   /home/mwilson/bin/awmcpuload \
#   /home/mwilson/bin/temp \
#   /home/mwilson/bin/net \
#   /home/mwilson/bin/i-net0 \
#   /home/mwilson/bin/i-net1 \
#   wmsysmon \
#   wmix'
#   (for X in $APPS ; do ($X &) ; sleep $DELAY ; done) &
#===============================================================
# echo 40 > /sys/devices/platform/smapi/BAT0/start_charge_thresh  #set start charge
# echo 70 > /sys/devices/platform/smapi/BAT0/stop_charge_thresh   #set stop

#if which dbus-launch >/dev/null && test -z "$DBUS_SESSION_BUS_ADDRESS"; then
           #eval "$(dbus-launch --sh-syntax --exit-with-session)"
#fi
#/usr/share/polkit-1/actions/org.freedesktop.devicekit.disks.policy
#<allow_any>yes</allow_any>

